﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Base.cshtml";
    var curuser = System.Web.HttpContext.Current.User.Identity.Name;
}
<input type="hidden" id="selprojectid" />
<input type="hidden" id="edittype" />
<div class="row-fluid">
<div class="span12">
<table border="0" width="100%" >
    <tr>
        <td width="8%" align="right">
            关键字:
        </td>
        <td width="30%">
        <input id="txtKey" name="txtKey"  type="text" class="input-medium search-query" placeholder="项目名称/项目代码/产品名称/担保公司" style="width:300px;
                background-color: #fff;"/>
        </td>
        <td width="8%" align="right">
            状态:
        </td>
        <td>
            <select id="ddlStatus" class="selectpicker" data-style="btn-success">
                <option value="-2">全部</option>
                <option value="-1">审核不通过</option>
                <option value="0">保存</option>
                <option value="1">提交</option>
                <option value="2">审核中</option>
                <option value="3">审核通过</option>
                <option value="4">已发布</option>
                <option value="5">流标</option>
                <option value="6">满标</option>
                <option value="7">融资成功</option>
                <option value="8">还款中</option>
                <option value="9">完毕</option>
            </select>
            <button class="btn btn-primary btn-small" id="btnSearch">
                查询
            </button>
        </td>
        <td align="right">
            <button id="btnAdd" class="btn btn-primary btn-small" style="margin-right: 20px">
                新增项目
            </button>
        </td>
    </tr>
</table>
</div>
</div>
<hr />
<table id="projectgrid" class="mmg">
    <tr>
        <th rowspan="" colspan="">
        </th>
    </tr>
</table>
<div id="pg" style="text-align: right;">
</div>

@section tail{
    <script src="/content/plugins/mmgrid/mmGrid.js" type="text/javascript"></script>
    <script src="/content/plugins/mmgrid/mmPaginator.js" type="text/javascript"></script>
    <script type="text/javascript">
        var cols = [
            { title: '项目编码', name: 'ProjectCode', width: 20, align: 'center' },
            { title: '项目名称', name: 'ProjectTitle', width: 65, align: 'center' },
            { title: '产品名称', name: 'ProductName', width: 40, align: 'center' },
            { title: '借款企业', name: 'EnterpriseName', width: 65, align: 'center', renderer: function (val, item, index) {
                return item["Enterprise"]["EnterpriseName"];
            }
            },
            { title: '借款金额', name: 'Amount', width: 15, align: 'center', renderer: function (val, item, index) {
                return "&yen" + Number(item["Financing"] / 10000).toFixed(2) + "万元";
            }
            },
            { title: '借款时长', name: 'Duration', width: 15, align: 'center', renderer: function (val, item, index) {
                var durationType = item["Policy"]["DurationType"] == 0 ? "天" : "月";
                return (item["CycleAmount"] * item["Policy"]["Duration"]) + durationType;
            }
            },
            { title: '状态', name: 'Status', width: 20, align: 'center', renderer: function (val, item, index) {
                if (parseInt(val) == -1) return '审核不通过';
                else if (parseInt(val) == 0) return '保存';
                else if (parseInt(val) == 1) return '提交';
                else if (parseInt(val) == 2) return '审核中';
                else if (parseInt(val) == 3) return '审核通过';
                else if (parseInt(val) == 4) return '已发布';
                else if (parseInt(val) == 5) return '流标';
                else if (parseInt(val) == 6) return '满标';
                else if (parseInt(val) == 7) return '融资成功';
                else if (parseInt(val) == 8) return '还款中';
                else if (parseInt(val) == 9) return '完毕';
            }
            },
            { title: '操作', name: '', width: 220, align: 'center', lockWidth: true, lockDisplay: true, renderer: function (val, item, index) {
                var html = '<button  class="btn btn-info btn-mini" onclick="projectShow(\'' + item["ID"] + '\', 2)">详情</button>';
                if (item["CreateUser"] == '@curuser') {
                    if (item["Status"] == 0) {
                        html += ' <button  class="btn btn-info btn-mini" onclick="projectShow(\'' + item["ID"] + '\', 1)">修改</button>';
                        html += ' <button  class="btn btn-info btn-mini" onclick="changeProjectStatus(\'' + item["ID"] + '\', 1)">审核</button>';
                        html += ' <button  class="btn btn-danger btn-mini" onclick="changeProjectStatus(\'' + item["ID"] + '\', 99)">删除</button>';
                    }
                }
                return html;
            }
            }
        ];

        var mmg;
        $(function () {
            $("#btnAdd").click(function () {
                location.href = decodeURIComponent("/Project/ProjectAdd");
            });

            mmg = $('#projectgrid').mmGrid({
                indexCol: true,
                indexColWidth: 25,
                height: $("body", parent.document).height() - 220,
                cols: cols,
                url: '/Project/GetProjectListByPage',
                params: { key: $("#txtKey").val() },
                //method: 'get',
                remoteSort: true,
                //items: items
                //sortName: 'SECUCODE',
                //sortStatus: 'asc',
                multiSelect: false,
                checkCol: false,
                fullWidthRows: true,
                autoLoad: false,
                plugins: [
                        $('#pg').mmPaginator()
                    ]
            });
            mmg.load();
            $("#btnSearch").click(function () {
                search();
            });
        });

        function validate() {
            $("#lblmsg").html("");
            var prodname = $("#txtPolicyName").val();
            if (prodname == "") {
                $("#txtPolicyName").focus();
                $("#lblmsg").html('政策名称不能为空');
                return false;
            }
            var prodnamereg = /^[A-Za-z0-9\u4e00-\u9fa5]+$/;
            if (!prodnamereg.test($("#txtPolicyName").val())) {
                $("#txtPolicyName").focus();
                $("#lblmsg").html('政策名称不能包含特殊符号');
                return false;
            }
            var durareg = /^[0-9]*[1-9][0-9]*$/;
            if (!durareg.test($("#txtDuration").val())) {
                $("#txtDuration").focus();
                $("#lblmsg").html('结算周期必须为正整数');
                return false;
            }
            var ratereg = /^(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d{1,2})?$/;   ///^\d+\.?\d*$/;
            if (!ratereg.test($("#txtRate").val()) || parseFloat($("#txtRate").val()) <= 0) {
                $("#txtRate").focus();
                $("#lblmsg").html('年化收益必须为浮点数字');
                return false;
            }
            return true;
        }

        function search() {
            mmg.setParm({ key: $("#txtKey").val(), status: $("#ddlStatus").val() });
            mmg.load({ page: 1 });
        }

        function projectShow(projectid, projectname, duration, settype, baltype, rate) {
            var projectdiv = $("#projectform");
            var modaltitle = $("#modaltitle");
            modaltitle.text("编辑政策");
            $("#txtPolicyName").val(projectname);
            $("#txtDuration").val(duration);
            $("#selSettlementType").val(settype);
            $("#selBalanceType").val(baltype);
            $("#txtRate").val(rate);
            $("#selprojectid").val(projectid);
            $("#edittype").val("2");
            $("#lblmsg").html("");
            projectdiv.modal();
        }

        // 改变产品状态
        function changeProjectStatus(projectid, destinatstatus) {
            var deststatus;
            if (destinatstatus == 1) {
                deststatus = "启用";
            }
            else if (destinatstatus == 0) {
                deststatus = "禁用";
            }
            else if (destinatstatus == -1) {
                deststatus = "删除";
            }

            bootbox.confirm("<span style='font-size:14px;'><i style='color:green;' class='icon-question-sign bigger-290'></i> &nbsp;&nbsp;确定要" + deststatus + "该记录吗？</span>", '300px', function (confirmed) {
                if (confirmed == true) {
                    $.ajax({
                        url: '/Policy/PolicyStatusChange',
                        data: "projectId=" + projectid + "&destinationStatus=" + destinatstatus,
                        type: "post",
                        cache: false,
                        success: function (result) {
                            if (result == "True") {
                                bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-ok-sign bigger-290'></i>   " + deststatus + "成功</span>", '300px');
                                mmg.load();
                            } else {
                                bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-remove-sign bigger-290'></i>   " + deststatus + "失败</span>", '300px');

                            }
                        }
                    });
                }
            });
        }

    </script>
}
